/**
 * 实际费用计算维护 - 编辑页面
 * @author miaoyl
 * @date 2019/01/18
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            AMOUNT: 0,
            form: {
            },
            rules: {
                GWFDDOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFD001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFD002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                gwdf002:app.datas_('crm_gwat550_qb')
            },
            total: {
                GWFD102_SUM: 0,
                GWFD106_SUM: 0,
                GWFD107_SUM: 0
            }
        },
        cols = [[
            {type: 'checkbox', title: '选择'}
            ,{ field: 'GWFDSEQ', width: 80, title: '项次'}
            , {field: 'GWFD003_NAME', width: 100,title: '区域', event: 'openGwfd003'}
            , {field: 'GWFD004_NAME',width: 120,title: '客户', event: 'openGwfd004'}
            , {field: 'GWFD005_NAME',width: 100,title: '品类', event: 'openGwfd005'}
            , {field: 'GWFD006_NAME',width: 100,title: '业务组织', event: 'openGwfd006'}
            , {field: 'GWFD007_NAME',width: 100,title: '费用', event: 'openGwfd007'}
            , {field: 'GWFD008',width: 100,title: '费用来源', edit: 'select',
                filterDatas: app.datas_('crm_gwft300_gwfd008')
            }
            , {field: 'GWFD100', width: 80,title: '币别', edit: 'text'}
            , {field: 'GWFD101', width: 80,title: '汇率', edit: 'text'}
            , {field: 'GWFD102', width: 160,title: '预测销售原币金额', edit: 'text'}
            , {field: 'GWFD103', width: 160,title: '预测销售本币金额', edit: 'text'}
            , {field: 'GWFD104', width: 80,title: '区域费率'/*, edit: 'text'*/}
            , {field: 'GWFD105', width: 80,title: '总部费率'/*, edit: 'text'*/}
            , {field: 'GWFD106', width: 160,title: '区域实际费用原币金额'/*, edit: 'text'*/}
            , {field: 'GWFD108', width: 160,title: '区域实际费用本币金额'/*, edit: 'text'*/}
            , {field: 'GWFD107', width: 160,title: '总部实际费用原币金额'/*, edit: 'text'*/}
            , {field: 'GWFD109', width: 160,title: '总部实际费用本币金额'/*, edit: 'text'*/}
            , {field: 'GWFDACTI', width: 80,title: '有效'}
            , {field: 'GWFD003', width: 80,hide: true}
            , {field: 'GWFD004', width: 80,hide: true}
            , {field: 'GWFD005', width: 80,hide: true}
            , {field: 'GWFD006', width: 80,hide: true}
            , {field: 'GWFD007', width: 80,hide: true}
        ]],
        callObj,
        moduleName,
        listData = null,
        table, gwft300Table, layOpenIndex
    ;

    var Gwft300Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwft300_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_window', tpl: 'common/_open_window' //0
                    }
                    ]
                }, win);
            },
            
            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwft300Table = table.render({
                        elem: app.getByid(doc, 'gwft300_edit_table'),
                        cellMinWidth: 80,
                        id: 'gwft300_edit_table',
                        defaultToolbar:false,
                        height: '400px',
                        cols: cols,
                        data: listData,
                        page: false
                    });

                    table.on('tool(gwft300_edit_table)', function (obj) {
                        if(obj.event == 'openGwfd003'){
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwad002',
                                        columnList: [{label: '区域编号', prop: 'GWAD002', width: '150'},
                                            {label: '区域名称', prop: 'GWADL004', width: '150'}]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            GWFD003_NAME: data.GWADL004,
                                            GWFD003: data.GWAD002
                                        })
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择区域信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }else if(obj.event == 'openGwfd004'){
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwak001',
                                        columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                            {label: '客户繁称', prop: 'GWAKL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            GWFD004_NAME: data.GWAKL004,
                                            GWFD004: data.GWAK001,
                                            GWFD100: data.GWAK008
                                        });
                                        app.ajax({
                                            xurl: 'crm/gwft300/getHuiLv',
                                            xdata:{GWEK001: data.GWAK008, GWEK002: (vueData.form.GWFD001+ vueData.form.GWFD002)},
                                            cb: function (result) {
                                                if(result.ok) {
                                                    var data = result.data.data;
                                                    obj.updateOfEdit({
                                                        GWFD101: data.GWEK005
                                                    });
                                                }else {
                                                    console.log("获取小数位失败");
                                                }
                                            }
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择客户信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }else if(obj.event == 'openGwfd005'){
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwao001',
                                        columnList: [{label: '系列编号', prop: 'GWAO001', width: '150'},
                                            {label: '系列名称', prop: 'GWAOL003', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            GWFD005_NAME: data.GWAOL003,
                                            GWFD005: data.GWAO001
                                        })
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择品类信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }else if(obj.event == 'openGwfd006'){
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysorg_id',
                                        columnList: [{label: '组织ID', prop: 'ID', width: '150'},
                                            {label: '组织名称', prop: 'NAME', width: '150'}
                                        ],
                                        searchParam:{
                                            TYPE: '3'
                                        }
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            GWFD006_NAME: data.NAME,
                                            GWFD006: data.ID
                                        })
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择业务组织信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }else if(obj.event == 'openGwfd007'){
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwfa001',
                                        columnList: [{label: '费用编号', prop: 'GWFA001', width: '150'},
                                            {label: '费用名称', prop: 'GWFAL003', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            GWFD007_NAME: data.GWFAL003,
                                            GWFD007: data.GWFA001,
                                            GWFD104: data.GWFA004,
                                            GWFD105: data.GWFA005
                                        });
                                        if(obj.data.GWFD102 != ''){
                                            var GWFD103 = obj.data.GWFD101 != '' ? self.calculate(math.mul(obj.data.GWFD101, obj.data.GWFD102), vueData.AMOUNT) : '';
                                            obj.updateOfEdit({
                                                GWFD103: GWFD103,
                                                GWFD106: data.GWFD104 != ''? self.calculate(math.mul(obj.data.GWFD102, obj.data.GWFD104), vueData.AMOUNT) : '',
                                                GWFD108: data.GWFD104 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, obj.data.GWFD104), vueData.AMOUNT) : '',
                                                GWFD107: data.GWFD105 != ''? self.calculate(math.mul(obj.data.GWFD102, obj.data.GWFD105), vueData.AMOUNT) : '',
                                                GWFD109: data.GWFD105 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, obj.data.GWFD105), vueData.AMOUNT): ''
                                                /*GWFD106: obj.data.GWFD104 != ''? self.calculate(math.mul(obj.value, math.div(obj.data.GWFD104, 100)), vueData.AMOUNT) : '',
                                                 GWFD108: obj.data.GWFD104 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, math.div(obj.data.GWFD104, 100)), vueData.AMOUNT) : '',
                                                 GWFD107: obj.data.GWFD105 != ''? self.calculate(math.mul(obj.value, math.div(obj.data.GWFD105, 100)), vueData.AMOUNT) : '',
                                                 GWFD109: obj.data.GWFD105 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, math.div(obj.data.GWFD105, 100)), vueData.AMOUNT): ''*/
                                            });
                                        }
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择业务组织信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                        self.calTotal(gwft300Table.getDatas());
                    });

                    //监听单元格编辑
                    table.on('edit(gwft300_edit_table)', function (obj) {
                        if (obj.field == 'GWFD102') {//预测销售原币金额
                            var parnt = /^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/;
                            if(!parnt.test(obj.value)){
                                app.showWarning("预测销售原币金额必须大于0！");
                                return;
                            }
                            var GWFD103 = obj.data.GWFD101 != '' ? self.calculate(math.mul(obj.data.GWFD101, obj.value), vueData.AMOUNT) : '';
                            obj.updateOfEdit({
                                GWFD103: GWFD103,
                                GWFD106: obj.data.GWFD104 != ''? self.calculate(math.mul(obj.value, obj.data.GWFD104), vueData.AMOUNT) : '',
                                GWFD108: obj.data.GWFD104 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, obj.data.GWFD104), vueData.AMOUNT) : '',
                                GWFD107: obj.data.GWFD105 != ''? self.calculate(math.mul(obj.value, obj.data.GWFD105), vueData.AMOUNT) : '',
                                GWFD109: obj.data.GWFD105 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, obj.data.GWFD105), vueData.AMOUNT): ''
                                /*GWFD106: obj.data.GWFD104 != ''? self.calculate(math.mul(obj.value, math.div(obj.data.GWFD104, 100)), vueData.AMOUNT) : '',
                                GWFD108: obj.data.GWFD104 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, math.div(obj.data.GWFD104, 100)), vueData.AMOUNT) : '',
                                GWFD107: obj.data.GWFD105 != ''? self.calculate(math.mul(obj.value, math.div(obj.data.GWFD105, 100)), vueData.AMOUNT) : '',
                                GWFD109: obj.data.GWFD105 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, math.div(obj.data.GWFD105, 100)), vueData.AMOUNT): ''*/
                            });
                        }else if(obj.field == 'GWFD101'){//汇率
                            var parnt = /^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/;
                            if(!parnt.test(obj.value)){
                                app.showWarning("汇率必须大于0！");
                                return;
                            }
                            if(obj.data.GWFD102 != ''){
                                var GWFD103 = self.calculate(math.mul(obj.data.GWFD102, obj.value), vueData.AMOUNT);
                                obj.updateOfEdit({
                                    GWFD103: GWFD103,
                                    GWFD106: obj.data.GWFD104 != '' ? self.calculate(math.mul(obj.data.GWFD102, obj.data.GWFD104), vueData.AMOUNT) : '',
                                    GWFD108: obj.data.GWFD104 != '' ? self.calculate(math.mul(GWFD103, obj.data.GWFD104), vueData.AMOUNT) : '',
                                    GWFD107: obj.data.GWFD105 != '' ? self.calculate(math.mul(obj.data.GWFD102, obj.data.GWFD105), vueData.AMOUNT) : '',
                                    GWFD109: obj.data.GWFD105 != '' ? self.calculate(math.mul(GWFD103, obj.data.GWFD105), vueData.AMOUNT): ''
                                    /*GWFD106: obj.data.GWFD104 != ''? self.calculate(math.mul(obj.value, math.div(obj.data.GWFD104, 100)), vueData.AMOUNT) : '',
                                     GWFD108: obj.data.GWFD104 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, math.div(obj.data.GWFD104, 100)), vueData.AMOUNT) : '',
                                     GWFD107: obj.data.GWFD105 != ''? self.calculate(math.mul(obj.value, math.div(obj.data.GWFD105, 100)), vueData.AMOUNT) : '',
                                     GWFD109: obj.data.GWFD105 != '' && GWFD103 != '' ? self.calculate(math.mul(GWFD103, math.div(obj.data.GWFD105, 100)), vueData.AMOUNT): ''*/
                                });
                            }
                        }else if(obj.field == 'GWFD103'){
                            var parnt = /^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/;
                            if(!parnt.test(obj.value)){
                                app.showWarning("预测销售本币金额必须大于0！");
                                return;
                            }
                            obj.updateOfEdit({
                                GWFD108: obj.data.GWFD104 != '' ? self.calculate(math.mul(obj.value, obj.data.GWFD104), vueData.AMOUNT) : '',
                                GWFD109: obj.data.GWFD105 != '' ? self.calculate(math.mul(obj.value, obj.data.GWFD105), vueData.AMOUNT): ''
                            });
                        }else if(obj.field == 'GWFD105'){
                            var parnt = /^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/;
                            if(!parnt.test(obj.value)){
                                app.showWarning("区域费率必须大于0！");
                                return;
                            }
                            obj.updateOfEdit({
                                GWFD107: obj.data.GWFD102 != ''? self.calculate(math.mul(obj.data.GWFD102, obj.value), vueData.AMOUNT) : '',
                                GWFD109: obj.data.GWFD103 != '' ? self.calculate(math.mul(obj.data.GWFD103, obj.data.GWFD105), vueData.AMOUNT): ''
                            });
                        }else if(obj.field == 'GWFD104'){
                            obj.updateOfEdit({
                                GWFD106: obj.data.GWFD102 != ''? self.calculate(math.mul(obj.data.GWFD102, obj.value), vueData.AMOUNT) : '',
                                GWFD108: obj.data.GWFD103 != '' ? self.calculate(math.mul(obj.data.GWFD103, obj.value), vueData.AMOUNT) : ''
                            });
                        }else if(obj.field == 'GWFD106'){
                            obj.updateOfEdit({
                                GWFD108: obj.data.GWFD101 != ''? self.calculate(math.mul(obj.data.GWFD101, obj.value), vueData.AMOUNT) : ''
                            });
                        }else if(obj.field == 'GWFD107'){
                            obj.updateOfEdit({
                                GWFD109: obj.data.GWFD101 != ''? self.calculate(math.mul(obj.data.GWFD101, obj.value), vueData.AMOUNT) : ''
                            });
                        }
                        self.calTotal(gwft300Table.getDatas());
                    });

                });
            },
            tableReload: function (listData) {
                self._reloadTable({id: 'gwft300_edit_table', data: listData});
            },

            calTotal:function (listData) {
                var total_data = {
                    GWFD102_SUM: 0,
                    GWFD106_SUM: 0,
                    GWFD107_SUM: 0
                };
                listData.forEach(function (item) {
                    total_data.GWFD102_SUM = math.add(total_data.GWFD102_SUM, item.GWFD102);
                    total_data.GWFD106_SUM = math.add(total_data.GWFD106_SUM, item.GWFD106);
                    total_data.GWFD107_SUM = math.add(total_data.GWFD107_SUM, item.GWFD107);
                });
                app.extendUpLow({
                    target: vueData.total,
                    res: total_data,
                    reset: true
                })
            },

            addData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwft300/addData',
                    xdata: listData,
                    toJsonStr: true,
                    okmsg: moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwft300').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            editData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwft300/editData',
                    xdata: listData,
                    toJsonStr: true,
                    okmsg: moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwft300').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            calculate:function (num,digit) {
                var data = math.div(Math.round(math.mul(num, digit)),digit);
                return data;
            },

            _call: function (tagData) {
                moduleName = tagData.moduleName;
                layOpenIndex = tagData.layOpenIndex;
                if (tagData.is_check) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    app.ajax({
                        xurl: 'crm/gwft300/getAddData',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: result.data.data,
                                    reset: true
                                });
                                var listData = [];
                                self.tableReload(listData);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    vueData.isCheck = tagData.is_check;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    app.ajax({
                        xurl: 'crm/gwft300/getGwfdData',
                        xdata: {GWFDDOCNO: callObj.data.GWFDDOCNO},
                        cb: function (result) {
                            if(result.ok){
                                if(result.data.data.length > 0){
                                    self.tableReload(result.data.data);
                                    self.calTotal(result.data.data);
                                }
                            }
                        }
                    });
                }
                app.ajax({
                    xurl: 'admin/getDigit',
                    cb: function (result) {
                        if(result.ok) {
                            var data = result.data.data;
                            vueData.AMOUNT = Math.pow(10,data.amount);
                        }else {
                            console.log("获取小数位失败");
                        }
                    }
                });
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['GWFD_T'],vueData.form);
                var vue = new Vue({
                    el: '#gwft300_edit',
                    data: vueData,
                    watch: {

                    },
                    methods: {
                        addData: function () {
                            if (vueData.form.GWFD001 == '' || vueData.form.GWFD001 == null) {
                                app.showWarning("请先选择年度！");
                                return;
                            }
                            if (vueData.form.GWFD002 == '' || vueData.form.GWFD002 == null) {
                                app.showWarning("请先选择月份！");
                                return;
                            }
                            var choose_data = gwft300Table.getDatas();
                            var count = 0;
                            if(choose_data.length > 0) {
                                count = choose_data[choose_data.length - 1].GWFDSEQ;
                            }

                            choose_data.push({
                                GWFDSEQ: count+1,
                                GWFD003_NAME: '',
                                GWFD004_NAME: '',
                                GWFD005_NAME: '',
                                GWFD006_NAME: '',
                                GWFD007_NAME: '',
                                GWFD008: '3',
                                GWFD100_NAME: '',
                                GWFD101: '',
                                GWFD102: '',
                                GWFD103: '',
                                GWFD104: '',
                                GWFD105: '',
                                GWFD106: '',
                                GWFD107: '',
                                GWFD108: '',
                                GWFD109: '',
                                GWFDACTI: 'N',
                                GWFD003: '',
                                GWFD004: '',
                                GWFD005: '',
                                GWFD006: '',
                                GWFD007: '',
                                GWFD100: ''
                            });
                            self.tableReload(choose_data);
                        },
                        cancle: function () {
                            layer.close(layOpenIndex);
                        },
                        delData: function () {
                            listData = gwft300Table.getDatas();
                            var checkStatus = table.checkStatus('gwft300_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    listData.removeByMap({
                                        GWFDSEQ: v.GWFDSEQ
                                    });
                                });
                                if (checkStatus.length != 0) {
                                    var i = 1;
                                    listData.forEach(function (v) {
                                        v.GWFDSEQ = i;
                                        i++;
                                    });
                                }
                                self.tableReload(listData);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var flag = true;
                                    var listData = gwft300Table.getDatas();
                                    if(listData.length < 1){
                                        app.showWarning('表格数据不能为空！');
                                        return;
                                    }
                                    /*for (var i = 0; i < listData.length; i++) {
                                        if(listData[i].MMRW003 <= 0){
                                            app.showWarning('数量必须大于0！');
                                            flag = false;
                                            return;
                                        }
                                    }*/
                                    listData.addMap(vueData.form, false);
                                    if(flag){
                                        if (vueData.isAdd) {
                                            self.addData(listData);
                                        } else {
                                            self.editData(listData);
                                        }
                                    }

                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwft300_edit');
                    }
                });
                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        });

    return new Gwft300Edit();

});